﻿var registerComps = (app, comps) => {
    for ([name, comp] of Object.entries(comps)) {
        app.component(name, comp);
    }
};

var deepCopy = (obj) => {
    if ((obj === null) || (typeof obj !== 'object')) {
        return obj;
    }

    if (obj instanceof Date) {
        return new Date(obj.getTime());
    }

    if (obj instanceof Array) {
        return obj.reduce((arr, item, i) => {
            arr[i] = deepCopy(item);
            return arr;
        }, []);
    }

    if (obj instanceof Object) {
        return Object.keys(obj).reduce((newObj, key) => {
            newObj[key] = deepCopy(obj[key]);
            return newObj;
        }, {});
    }
};

var getQueryParams = () => {
    var params = {};
    var queryString = window.location.search.substring(1); // 去掉问号
    var vars = queryString.split('&');
    for (var i = 0; i < vars.length; i++) {
        if (!vars[i]) continue;
        var pair = vars[i].split('=');
        params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
    }
    return params;
};

var removeUrlParams = () => {
    var newUrl = window.location.pathname + window.location.hash;
    window.history.replaceState(null, '', newUrl);
};

var dateFormat = (date, format) => {
    return new dayjs(date).format(format);
};